package com.mz361.stdapi.service.impl;

import com.mz361.stdapi.domain.StandardTable;
import com.mz361.stdapi.repository.StandardTableRepository;
import com.mz361.stdapi.service.StandardTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StandardTableServiceImpl implements StandardTableService {
    @Autowired
    StandardTableRepository standardTableRepository;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public Page<StandardTable> getPageList(Example<StandardTable> example, Integer pageIndex, Integer pageSize) {
        Sort sort = Sort.by("id");
        PageRequest page = PageRequest.of(pageIndex - 1, pageSize, sort);
        return standardTableRepository.findAll(page);
    }

    @Override
    public List<StandardTable> getAll() {
        return standardTableRepository.findAll();
    }

    @Override
    public Page<StandardTable> getAllByStandClass(String standardClass, Integer pageIndex, Integer pageSize) {
        PageRequest page = PageRequest.of(pageIndex - 1, pageSize);
        return standardTableRepository.findAllByStandClass(standardClass, page);
    }

    @Override
    public void saveAll(List<StandardTable> standardTableList) {
        standardTableRepository.saveAll(standardTableList);
    }

    @Override
    public StandardTable findById(Integer id) {
        return null;
    }

    @Override
    public void deleteById(Integer id) {

    }

    @Override
    public void updateById(StandardTable order) {

    }

    @Override
    public Page<StandardTable> findList(StandardTable standardTable, Integer pageIndex, Integer pageSize) {
        CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()
                .and(new Criteria("fn_A301").contains(standardTable.getFn_A301()))
                .and(new Criteria("fn_A100").is(standardTable.getFn_A100())))
                .setPageable(PageRequest.of(pageIndex, pageSize));

        SearchHits<StandardTable> searchHits = elasticsearchRestTemplate.search(criteriaQuery, StandardTable.class);
        List<StandardTable> result = searchHits.get().map(SearchHit::getContent).collect(Collectors.toList());
        Pageable pageable = PageRequest.of(pageIndex, pageSize);
        return new PageImpl(result, pageable, searchHits.getTotalHits());
    }

    @Override
    public Page<StandardTable> findAll(Integer pageIndex, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageIndex, pageSize);
        return standardTableRepository.findAll(pageable);
    }

    @Override
    public Page<StandardTable> findHighlight(StandardTable order, Integer pageIndex, Integer pageSize) {
        return null;
    }
}
